#import <SpringBoard/SBApplication.h>
#import <SpringBoard/SBApplicationController.h>
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#import <objc/runtime.h>
#import <UIKit/UIApplication.h>
#import "../LockInfo/Plugin.h"

#import "framework/FWCommon.h"
#import "framework/FWApplication.h"
#import "framework/FWIconDotView.h"
#import "framework/FWDotView.h"
#import "framework/FWRoundedLabel.h"

#import "PITodoView.h"
#import "PIPlugin.h"
#import "PIIntegration.h"


@implementation PITodoView

@synthesize action, flag, context;


// ******************************************************************************************
#pragma mark -
#pragma mark Initialisation and object management

- (id)initWithFrame:(CGRect)frame table:(LITableView *)table application:(FWApplication*)app
{
    if (self = [super initWithFrame:frame table:table]) {
        self.action = [[[FWIconDotView alloc] initWithFrame:CGRectMake(0, 0, kFWListViewMarkerSpace, kFWListViewMarkerSpace)
                                                      inset:(kFWListViewMarkerSpace - kFWListViewMarkerSize) / 2] autorelease];
        self.action.backgroundColor = [UIColor clearColor];
        
        NSString* flagPath = [app.applicationPath stringByAppendingString:@"/star_yellow_small.png"];
        self.flag = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kFWListViewMarkerSize, kFWListViewMarkerSize)] autorelease];
        self.flag.image = [UIImage imageWithContentsOfFile:flagPath];
        self.flag.backgroundColor = [UIColor clearColor];
        
        NSString* contextPath = [app.pluginBundle pathForResource:@"context" ofType:@"png"];
        self.context = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kFWListViewMarkerSize, kFWListViewMarkerSize)] autorelease];
        self.context.image = [UIImage imageWithContentsOfFile:contextPath];
        self.context.backgroundColor = [UIColor clearColor];
        
        [self addSubview:self.context];
        [self addSubview:self.flag];
        [self addSubview:self.action];
        
        self.flags = [NSArray arrayWithObjects:
                      self.action,
                      self.flag,
                      self.context,
                      nil];
    }
    
    return self;
}

-(void)dealloc 
{
    [action release];
    [flag release];
    [context release];
    
    [super dealloc];
}


// ******************************************************************************************
#pragma mark -
#pragma mark Updating and presenting information to the user

- (void)setViewForTable:(LITableView*)table application:(FWApplication*)app title:(NSString*)title due:(NSString*)dates dueShort:(NSString*)shortDates overdue:(BOOL)overdue importance:(int)importance priority:(NSString*)prty action:(int)actn flagged:(BOOL)flagged context:(BOOL)ctx showIcons:(BOOL)showIcons 
{
    self.summaryText.style = [[table.theme.summaryStyle copy] autorelease];
    self.summaryText.text = title;
    
    UIColor* importanceColor = [PIIntegration colorForImportance:importance application:app];
    UIColor* importanceBGColor = [PIIntegration colorBGForImportance:importance application:app];
    if (importanceBGColor != nil) {
        self.dot.color = importanceBGColor;
        self.dot.hidden = false;
        [self.dot setNeedsDisplay];
    }
    else
        self.dot.hidden = true;
    
    if ((dates != nil) && (prty != nil) && ([prty length] > 0)) {
        self.highlightText.label.text = prty;
        self.highlightText.label.font = table.theme.detailStyle.font;
        self.highlightText.label.textColor = importanceColor;
        //[self.highlightText setRoundedColor:[importanceBGColor colorWithAlphaComponent:0.85]];
        [self.highlightText setRoundedColor:importanceBGColor];
        self.highlightText.hidden = false;
    }
    else
        self.highlightText.hidden = true;

    if (flagged && showIcons) {
        self.flag.hidden = false;
        [self.flag setNeedsDisplay];
    }
    else
        self.flag.hidden = true;
    
    if (ctx && showIcons) {
        self.context.hidden = false;
        [self.context setNeedsDisplay];
    }
    else
        self.context.hidden = true;
    
    if (dates != nil) {
        if (overdue) {
            self.detailText.style = [[table.theme.detailStyle copy] autorelease];
            self.detailText.style.textColor = [PIIntegration colorForOverdue:app];
        }
        else
            self.detailText.style = [[table.theme.detailStyle copy] autorelease];
        
        self.detailText.text = dates;
        self.detailText.hidden = false;
        self.detailTextAlternate = shortDates;
    }
    else
        self.detailText.hidden = true;
    
    NSString* actionIcon = [PIIntegration iconPathForAction:actn application:app];
    if (showIcons && (actionIcon != nil)) {
        self.action.color = [PIIntegration colorBGForAction:actn application:app];
        self.action.icon = actionIcon;
        [self.action setNeedsDisplay];
        self.action.hidden = false;
    }
    else
        self.action.hidden = true;
    
    [self setNeedsLayout];
}

@end
